#!/bin/bash
#################################################
# Copyright (C) 2012, 2013   Michael Haberler <license AT mah DOT priv DOT at>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
#################################################

usage () { 
cat << _EOF_ 
Usage: $0 [options] pru-binary-image
  -p <pru> PRU to target
_EOF_
}

while getopts 'p:' option
do
	case $option in
		p  ) PRU=$OPTARG;;
		\? ) echo "Unknown option: -$OPTARG" >&2; exit 1;;
		:  ) echo "Missing option argument for -$OPTARG" >&2; exit 1;;
		*  ) echo "Unimplimented option: -$OPTARG" >&2; exit 1;;
	esac
done

shift $(($OPTIND - 1))

if [ -z "$1" ] ; then
	usage
	exit 1
fi

if [ ! -r "$1" ] ; then
	echo "Cannot read PRU binary file: $1"
	exit 1
fi

DBGFILE="${1%.bin}.dbg"
if [ ! -r "$DBGFILE" ] ; then
	echo "Warning: Cannot read debug file: $DBGFILE"
fi

if [ ! -r /sys/class/uio/uio0 ] ; then
	echo "PRU control files not found in /sys/class/uio/uio0" >&2
	echo "Please make sure you have a proper device tree overlay loaded" >&2
	echo "Try: sudo su -c \"echo BB-BONE-PRU-01 > /sys/devices/bone_capemgr.*/slots\"" >&2
	exit 1;
fi

UIPATH="$EMC2_HOME/configs/ARM/PRU-Debugger"

halcmd -f << _EOF_ > /dev/null

# this loads the PRU binary 'pruexample.bin' into PRU 0
# and leaves the PRU disabled:
# see the Makefile how pruexample.bin is created

loadrt hal_pru prucode="$1" disabled=1 pru=$PRU

# this is the PRU debugger. It connects to PRU shared memory
# can inspect registers and PRU status (processor status, program counter)
# exported pins:
#
# hal_prudebug.reset       # u32,in  select PRU (0,1)
# hal_prudebug.continuous  # bit,in, sample PRU every thread period
# hal_prudebug.snap        # bit,in, sample PRU on positive edge
# hal_prudebug.halt        # bit,in, stop the PRU at current instruction
# hal_prudebug.step        # bit,in, set PRU stepping mode
# hal_prudebug.continue    # bit,in, reenable  PRU after halt; will step 1 instruction if stepping
# hal_prudebug.reset       # bit,in  reset PRU
#
# hal_prudebug.CONTROL     # u32,out PRU status register
# hal_prudebug.PC          # u32,out PRU program counter
# hal_prudebug.R0-R31      # u32,out PRU register values

# unrelated to debugging, see EXPLORE_COUNTERS #define in haö_prudebug.c
# hal_prudebug.IEP_COUNT     # u32,out 200Mhz TSC counter in IEP (Industrial Ethernet) module

loadrt hal_prudebug

loadrt threads period1=20000000 # 20msec
addf hal_pru.update thread1
addf hal_prudebug.update thread1
#


# sample PRU status every thread period
#setp hal_prudebug.continuous 1
#show pin hal_prudebug.ECAP_COUNT

# show last sample of program counter and registers
#show pin hal_prudebug.PC
#show pin hal_prudebug.R

loadusr -Wn ui gladevcp -u $UIPATH/ui.py -c ui -U filename="$DBGFILE" $UIPATH/prudebug.ui

net pru ui.pru hal_prudebug.pru
net continuous ui.continuous hal_prudebug.continuous
net snap ui.snap hal_prudebug.snap
net halt ui.halt hal_prudebug.halt

net stepping ui.stepping hal_prudebug.step
net continue ui.continue hal_prudebug.continue
net reset ui.reset hal_prudebug.reset

# hal_prudebug.CONTROL     # u32,out PRU status register
# hal_prudebug.PC          # u32,out PRU program counter
# hal_prudebug.R0-R31      # u32,out PRU register values

net control hal_prudebug.CONTROL  ui.CONTROL
net pc hal_prudebug.PC ui.PC
net r0 hal_prudebug.R0 ui.R0
net r1 hal_prudebug.R1 ui.R1
net r2 hal_prudebug.R2 ui.R2
net r3 hal_prudebug.R3 ui.R3
net r4 hal_prudebug.R4 ui.R4
net r5 hal_prudebug.R5 ui.R5
net r6 hal_prudebug.R6 ui.R6
net r7 hal_prudebug.R7 ui.R7
net r8 hal_prudebug.R8 ui.R8
net r9 hal_prudebug.R9 ui.R9
net r10 hal_prudebug.R10 ui.R10
net r11 hal_prudebug.R11 ui.R11
net r12 hal_prudebug.R12 ui.R12
net r13 hal_prudebug.R13 ui.R13
net r14 hal_prudebug.R14 ui.R14
net r15 hal_prudebug.R15 ui.R15
net r16 hal_prudebug.R16 ui.R16
net r17 hal_prudebug.R17 ui.R17
net r18 hal_prudebug.R18 ui.R18
net r19 hal_prudebug.R19 ui.R19
net r20 hal_prudebug.R20 ui.R20
net r21 hal_prudebug.R21 ui.R21
net r22 hal_prudebug.R22 ui.R22
net r23 hal_prudebug.R23 ui.R23
net r24 hal_prudebug.R24 ui.R24
net r25 hal_prudebug.R25 ui.R25
net r26 hal_prudebug.R26 ui.R26
net r27 hal_prudebug.R27 ui.R27
net r28 hal_prudebug.R28 ui.R28
net r29 hal_prudebug.R29 ui.R29
net r30 hal_prudebug.R30 ui.R30
net r31 hal_prudebug.R31 ui.R31
setp ui.register_table 1


start


waitusr ui
stop
unloadrt all

_EOF_

